package cn.sinata.wanglin.neighbor.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.hyphenate.chat.EMClient;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.DialogUtils;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.view.TwoTextLinearView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.db.dao.GroupDao;
import cn.sinata.wanglin.db.dao.UserDao;
import cn.sinata.wanglin.model.Group;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class GroupSettingActivity extends TitleActivity {

    @BindView(R.id.tv_doNot_disturb)
    TextView tv_doNot_disturb;
//    @BindView(R.id.ttlv_qr)
//    TwoTextLinearView ttlv_qr;
    @BindView(R.id.ttlv_nick)
    TwoTextLinearView ttlv_nick;
    @BindView(R.id.tv_invite)
    TextView tv_invite;
    @BindView(R.id.action_quite)
    TextView action_quite;
    @BindView(R.id.tv_edit)
    TextView tv_edit;

    private String nick;
    private String id;
    private String userId;
    private String name;
    private String image;
    private String content;
    private String account;
    private boolean isEdit = false;
    private int status ;
    private boolean isOwner = false;

    @Override
    protected int setContentId() {
        return R.layout.activity_group_setting;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("群设置");

        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        id = getIntent().getStringExtra("id");
        account = getIntent().getStringExtra("account");
        nick = getIntent().getStringExtra("groupNick");
        name = getIntent().getStringExtra("name");
        image = getIntent().getStringExtra("image");
        content = getIntent().getStringExtra("content");
        status = getIntent().getIntExtra("status",0);
        isOwner = getIntent().getBooleanExtra("isOwner",false);

        ttlv_nick.setRightText(nick);

        ttlv_nick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtil.create(mContext)
                        .go(EditGroupNickActivity.class)
                        .put("id",id)
                        .put("nick",nick)
                        .put("groupAccount",account)
                        .startForResult(0);
            }
        });

        tv_doNot_disturb.setSelected(status == 1);
        action_quite.setText(isOwner?"退出并解散群":"退出该群");

        tv_edit.setVisibility(isOwner?View.VISIBLE:View.GONE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0) {
                if (data != null) {
                    nick = data.getStringExtra("nick");
                    ttlv_nick.setRightText(nick);
                    isEdit = true;
                }
            } else if (requestCode == 3) {
                isEdit = true;
                finish();
            } else if (requestCode == 1) {
                isEdit = true;
                finish();
            }
        }
    }

    void quiteGroup(){
        showDialog();
        HttpManager.removeGroupMember(userId,id,account)
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        //防止冲突
                        isEdit = false;
                        EMClient.getInstance().chatManager().deleteConversation(account, true);
                        setResult(Constant.ResultCode_QUITE);
                        finish();
                    }
                });
    }

    void disbandGroup(){
        showDialog();
        HttpManager.disbandGroup(userId,id)
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        isEdit = false;
                        EMClient.getInstance().chatManager().deleteConversation(account, true);
                        setResult(Constant.ResultCode_QUITE);
                        finish();
                    }
                });
    }

    @Override
    public void finish() {
        if (isEdit) {
            setResult(RESULT_OK);
        }
        super.finish();
    }

    private void editGroupMsgSet(final boolean selected){
        showDialog();
        HttpManager.editGroupMsgSet(userId,id,selected?0:1)
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        if (selected) {
                            status = 0;
                        } else {
                            status = 1;
                        }
                        tv_doNot_disturb.setSelected(status==1);
                        Group group = new Group();
                        group.setGroupName(name);
                        group.setGroupImg(image);
                        group.setGroupAccount(account);
                        group.setMsgStatus(status);
                        group.setGroupNickName(nick);
                        group.setId(id);
                        GroupDao.getInstance(mContext).saveGroup(userId,group);
                        isEdit = true;
                    }
                });
    }

    @OnClick({R.id.tv_doNot_disturb,R.id.tv_invite,R.id.action_quite,R.id.tv_edit})
    void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.tv_doNot_disturb:
                boolean selected =view.isSelected();
                editGroupMsgSet(selected);
                break;
            case R.id.tv_invite:
                ActivityUtil.create(this)
                        .go(InviteMemberActivity.class)
                        .put("id",id)
                        .put("account",account)
                        .startForResult(1);
                break;
            case R.id.tv_edit:
                ActivityUtil.create(mContext)
                        .go(EditGroupInfoActivity.class)
                        .put("account",account)
                        .put("id",id)
                        .put("image",image)
                        .put("name",name)
                        .put("content",content)
                        .startForResult(3);
                break;
            case R.id.action_quite:
                String msg = "你确定要退出该群吗？";
                if (isOwner) {
                    msg = "你确定要退出并解散该群吗?";
                }
                DialogUtils.createNoticeDialog(mContext, "请注意",msg , "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (isOwner) {
                            disbandGroup();
                        } else {
                            quiteGroup();
                        }
                    }
                });
                break;
        }
    }
}
